package net.lab1024.sa.base.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class TextConversionUtils {

    private static final DateTimeFormatter TARGET_FORMAT =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter[] SOURCE_FORMATTERS = {
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSZ"),
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX"),
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"),
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    };
    private static final Logger log = LoggerFactory.getLogger(TextConversionUtils.class);

    public static String cnversion(String s) {

        String str = s.replaceAll(" and ", " && ").replaceAll(" or ", " || ").replaceAll(" AND ", " && ").replaceAll(" OR ", "||");
        return str;
    }



    public static String convertToStandardFormat(Object timeValue) {
        if (timeValue == null) {
            return null;
        }

        try {
            // 处理 Long 类型时间戳（秒或毫秒）
            if (timeValue instanceof Long) {
                return handleTimestamp((Long) timeValue);
            }

            // 处理 String 类型
            if (timeValue instanceof String) {
                String timeStr = ((String) timeValue).trim();

                // 尝试解析为数字时间戳
                if (timeStr.matches("\\d+")) {
                    return handleTimestamp(Long.parseLong(timeStr));
                }

                // 尝试各种日期格式
                return handleDateTimeString(timeStr);
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("无法转换时间值: " + timeValue, e);
        }

        throw new IllegalArgumentException("不支持的时间类型: " + timeValue.getClass());
    }

    private static String handleTimestamp(long timestamp) {
        // 判断是秒级还是毫秒级时间戳（以 2000-01-01 为分界点）
        if (timestamp < 10_000_000_000L) { // 10位秒级时间戳
            return formatInstant(Instant.ofEpochSecond(timestamp));
        } else { // 13位毫秒级时间戳
            return formatInstant(Instant.ofEpochMilli(timestamp));
        }
    }

    private static String handleDateTimeString(String timeStr) {
        if(timeStr.contains("T")){
            return timeStr;
        }
        // 尝试各种预定义的日期格式
        for (DateTimeFormatter formatter : SOURCE_FORMATTERS) {
            try {
                // 处理带时区的格式
                if (timeStr.contains("Z") || timeStr.contains("X")) {
                    ZonedDateTime zdt = ZonedDateTime.parse(timeStr, formatter);
                    return zdt.format(TARGET_FORMAT);
                }
                // 处理不带时区的格式
                else {
                    LocalDateTime ldt = LocalDateTime.parse(timeStr, formatter);
                    return ldt.format(TARGET_FORMAT);
                }
            } catch (DateTimeParseException ignored) {
                // 尝试下一个格式
//                throw new DateTimeParseException("无法解析的日期格式", timeStr, 0);
//                log.error("无有解析日期==============================="+timeStr);

            }
        }
        return timeStr;

    }

    private static String formatInstant(Instant instant) {
        return instant.atZone(ZoneId.systemDefault())
                .format(TARGET_FORMAT);
    }

//    // 测试用例
//    public static void main(String[] args) {
//        // 测试各种格式
//        System.out.println(convertToStandardFormat(1765000000L));      // 秒级时间戳
//        System.out.println(convertToStandardFormat(1765000000000L));   // 毫秒级时间戳
//        System.out.println(convertToStandardFormat("1765000000"));     // 字符串秒级
//        System.out.println(convertToStandardFormat("1765000000000"));  // 字符串毫秒级
//        System.out.println(convertToStandardFormat("2025-09-11 14:23:23.000+0800"));
//        System.out.println(convertToStandardFormat("2025-09-11 14:23:23"));
//    }

}
